package cn.tedu.mall.seckill.controller;

import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.pojo.seckill.dto.SeckillOrderAddDTO;
import cn.tedu.mall.pojo.seckill.vo.SeckillCommitVO;
import cn.tedu.mall.seckill.service.ISeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 只负责一项功能
 * 秒杀订单提交
 */
@RestController
public class SeckillCommitController {
    @Autowired
    private ISeckillService seckillService;

    @PostMapping("/seckill/{randCode}")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<SeckillCommitVO> commitSeckill(
            @PathVariable String randCode,
            SeckillOrderAddDTO seckillOrderAddDTO) {
        //sn没有从前端传递
        String sn = UUID.randomUUID().toString();
        //路径参数,封装到dto对象,交给业务层处理对比逻辑
        seckillOrderAddDTO.setSn(sn);
        seckillOrderAddDTO.setRandCode(randCode);
        SeckillCommitVO commitVO = seckillService.commitSeckill(seckillOrderAddDTO);
        return JsonResult.ok(commitVO);
    }
}
